import http from '../utils/request.js';
import config from '../utils/config.js';

// Upload API service
const uploadApi = {
    /**
     * Upload product image
     * @param {String} filePath - File path
     * @returns {Promise}
     */
    uploadProductImage(filePath, cloudPath = null) {
        // 对于微信小程序云开发环境，可以直接使用wx.cloud.uploadFile
        // #ifdef MP-WEIXIN
        if (config.useCloudStorage) {
            return http.upload(config.API.product.upload, filePath, {}, {
                cloudPath: cloudPath || `productsImage/${Date.now()}_${Math.floor(Math.random()*1000)}${filePath.substring(filePath.lastIndexOf('.'))}`,
                useCloudStorage: true
            });
        }
        // #endif
        
        // 对于其他环境，使用标准上传
        return http.upload(config.API.product.upload, filePath);
    },
    
    /**
     * Upload user avatar
     * @param {String} filePath - File path
     * @returns {Promise}
     */
    uploadAvatar(filePath, cloudPath = null) {
        // 对于微信小程序云开发环境，可以直接使用wx.cloud.uploadFile
        // #ifdef MP-WEIXIN
        if (config.useCloudStorage) {
            return http.upload(config.API.user.uploadAvatar, filePath, {}, {
                cloudPath: cloudPath || `avatars/${Date.now()}_${Math.floor(Math.random()*1000)}${filePath.substring(filePath.lastIndexOf('.'))}`,
                useCloudStorage: true
            });
        }
        // #endif
        
        // 对于其他环境，使用标准上传
        return http.upload(config.API.user.uploadAvatar, filePath);
    }
};

export default uploadApi; 